package com.bl.basic.controller;

import com.bl.basic.exception.BusinessException;
import com.bl.basic.util.BaiduAuditUtils;
import com.bl.basic.util.FastDfsUtil;
import com.bl.basic.util.JsonResult;
import com.bl.org.domain.Shop;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.lang.annotation.ElementType;

@RestController
@RequestMapping("/fastDfs")
public class FastDfsController {
    /**
     * 文件上传的接口
     * @param file
     * @return
     */
    @PostMapping
    public JsonResult upload(@RequestPart(value = "file",required = true )  MultipartFile file){
        try {
            // 获取原始文件名
            String filename = file.getOriginalFilename();
            // 截取文件名的后缀不需要点所以+1
            String substring = filename.substring(filename.lastIndexOf(".") + 1);

            // 文件上传的路径
            String path = FastDfsUtil.upload(file.getBytes(), substring);

            // 返回上传的路径
            return JsonResult.me().setResultObj(path);

        } catch (Exception e) {
           e.printStackTrace()  ;
            return JsonResult.me().setMessage("上传失败" + e.getMessage());
        }
    }
    /**
     * 文件删除接口
     */
    @DeleteMapping
    public  JsonResult delete(@RequestParam(value = "path",required = true) String path ){

        try {
            String filepath = path.substring(1);
            System.out.println("**************************************************************************1 : "+ filepath);
            String groupName1 = filepath.substring(0, filepath.indexOf("/"));
            System.out.println("**************************************************************************2 : "+ groupName1);
            String fileName1 = filepath.substring(filepath.indexOf("/") + 1);
            System.out.println("**************************************************************************3 : "+ fileName1);
            FastDfsUtil.delete(groupName1,fileName1);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMessage("删除失败");
        }
    }

}
